package com.ch.tools.client.simulator.controller;


import com.ch.ms.server.packet.LoginRequestPacket;
import com.ch.tools.client.MapleClient;
import com.ch.tools.client.simulator.LoginLauncher;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
@Slf4j
public class LoginLauncherViewController implements Initializable {

    @FXML
    private TextField accountTextField;


    @FXML
    private TextField passwordTextField;

    @FXML
    private Button loginButton;


    private final MapleClient mapleClient;


    @Autowired
    public LoginLauncherViewController(MapleClient mapleClient) {
        this.mapleClient = mapleClient;
    }


    @FXML
    private void onLoginButtonAction() {
        mapleClient.send(LoginRequestPacket.builder().account(accountTextField.getText()).password(passwordTextField.getText()).build());
    }

    @Value("${chms.servers.login.host}")
    private String loginServerHost;

    @Value("${chms.servers.login.port}")
    private int loginServerPort;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        LoginLauncher.getStage().setTitle("登录冒险岛");
        try {
            mapleClient.connect(loginServerHost, loginServerPort);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
